package cn.chenyumeng.blog.service.admin.impl;

import cn.chenyumeng.blog.dto.CommentDTO;
import cn.chenyumeng.blog.entity.Blog;
import cn.chenyumeng.blog.entity.Comment;
import cn.chenyumeng.blog.mapper.BlogMapper;
import cn.chenyumeng.blog.mapper.CommentMapper;
import cn.chenyumeng.blog.service.admin.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @program: blog->CommentServiceImpl
 * @description:
 * @author: cg
 * @create: 2020-07-26 23:13
 **/
@Service
public class CommentServiceImpl implements CommentService {
    
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private BlogMapper blogMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Transactional
    @Override
    public Comment queryById(Long id) {
        return this.commentMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Transactional
    @Override
    public List<Comment> queryAllByLimit(int offset, int limit) {
        return this.commentMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param comment 实例对象
     * @return 实例对象
     */
    @Transactional
    @Override
    public Comment insert(Comment comment) {
        this.commentMapper.insert(comment);
        return comment;
    }

    /**
     * 修改数据
     *
     * @param comment 实例对象
     * @return 实例对象
     */
    @Transactional
    @Override
    public Comment update(Comment comment) {
        this.commentMapper.update(comment);
        return this.queryById(comment.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Transactional
    @Override
    public boolean deleteById(Long id) {
        Long blogId = commentMapper.queryBlogIdByCommentId(id);
        if (commentMapper.deleteById(id) > 0) {
            Blog blog = blogMapper.queryById(blogId);
            blog.setCommentCount(commentMapper.queryBlogCommentCountByBlogId(blogId));
            blogMapper.update(blog);
            return true;
        }
        return false;
    }

    @Override
    public PageInfo<List<CommentDTO>> getCommentListByBlogId(Long id, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Comment> comments = commentMapper.queryParentCommentByBlogId(id);
        PageInfo pageInfo = new PageInfo(comments);
        List<CommentDTO> list = new ArrayList<>();
        pageInfo.getList().forEach(comment -> {
            CommentDTO dto = new CommentDTO();
            BeanUtils.copyProperties(comment, dto);
            replyComments = new ArrayList<>();
            getChildComments(dto);
            // 时间排序
            replyComments.sort(Comparator.comparing(CommentDTO::getCreateTime));
            dto.setChildCommentList(replyComments);
            list.add(dto);
        });
        pageInfo.setList(list);
        return pageInfo;
    }

    private List<CommentDTO> replyComments = new ArrayList<>();

    private void getChildComments(CommentDTO commentDTO) {
        List<Comment> comments = commentMapper.queryChildCommentByParentCommentId(commentDTO.getId());
        comments.forEach(comment -> {
            CommentDTO dto = new CommentDTO();
            BeanUtils.copyProperties(comment, dto);
            replyComments.add(dto);
            getChildComments(dto);
        });
    }

}
